<template>
  <div class="event-info">
    <el-form :model="ruleForm" :rules="rules" ref="UserForm" label-width="120px" label-suffix="：">
      <div class="event-content">
        <el-form-item label="友情链接名称" prop="blogrollName">
          <div class="cont-input">
            <el-input v-model="ruleForm.blogrollName" placeholder="请输入友情链接名称"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="链接地址" prop="blogroll">
          <div class="cont-input">
            <el-input v-model="ruleForm.blogroll" placeholder="请输入友情链接链接"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <div class="cont-input">
            <el-input v-model="ruleForm.remark" type="textarea" placeholder="请输入备注"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="isActivity">
          <div class="cont-input">
            <el-switch
              v-model="ruleForm.isActivity"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </div>
        </el-form-item>
      </div>
      <div class="cont-footer">
        <el-button @click.native="cancelPop">取消</el-button>
        <template v-if="commonTabInfo.text === '新增'">
          <el-button type="primary" @click.native="addBlogrollEvent">保存</el-button>
        </template>
        <template v-else-if="commonTabInfo.text === '编辑'">
          <el-button type="primary" @click.native="editBlogrollEvent">保存编辑</el-button>
        </template>
      </div>
    </el-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { AddOrUpdateBlogroll, GetBlogrollSingle } from '@/api/requestConfig'
  import UpLoad from '@/components/Upload'
  export default {
    name: 'UserInfo',
    data() {
      return {
        ruleForm: {
          isActivity: 1,
          blogrollName: '',
          blogroll: '',
          remark: ''
        },
        rules: {
          blogrollName: [
            { required: true, message: '请输入友情链接名称', trigger: 'blur' }
          ],
          blogroll: [
            { required: true, message: '请输入链接地址', trigger: 'blur' }
          ]
        },
        editData: {},
        addFlag: true
      }
    },
    computed: {
      ...mapGetters([
        'commonTabInfo'
      ])
    },
    components: {
      UpLoad
    },
    created() {
      // 编辑时获取数据
      if (this.commonTabInfo.text === '编辑') {
        GetBlogrollSingle(this.commonTabInfo.custom.id).then(res => {
          if (res.data) {
            const resData = res.data
            this.editData = resData
            if (this.editData) {
              for (var key in this.ruleForm) {
                this.ruleForm[key] = this.editData[key]
              }
            }
          }
        })
      }
    },
    methods: {
      // tabs操作
      eventTab(text, tabsName, custom) {
        this.$store.dispatch('handleTabInfo', {
          text: text,
          tabsName: tabsName,
          custom: custom
        })
      },
      // 取消
      cancelPop() {
        this.eventTab('', 'first')
      },
      // 新增
      addBlogrollEvent() {
        var self = this
        if (!this.addFlag) {
          return
        }
        this.$refs.UserForm.validate(function(valid) {
          if (valid) {
            var needData = {}
            needData.blogrollName = self.ruleForm.blogrollName
            needData.blogroll = self.ruleForm.blogroll
            needData.remark = self.ruleForm.remark
            needData.isActivity = self.ruleForm.isActivity
            self.addFlag = false
            AddOrUpdateBlogroll(needData).then(res => {
              if (res.code === 200) {
                self.$message.success(res.message)
                setTimeout(function() {
                  self.eventTab('refresh', 'first', 'blogroll')
                }, 500)
              } else {
                self.$message.error(res.message)
              }
            }).catch(error => {
              console.log(error)
            })
          } else {
            return
          }
        })
        setTimeout(function() {
          self.addFlag = true
        }, 3000)
      },
      // 编辑
      editBlogrollEvent() {
        var self = this
        if (!this.addFlag) {
          return
        }
        this.$refs.UserForm.validate(function(valid) {
          if (valid) {
            var needData = {}
            needData.id = self.editData.id
            needData.blogrollName = self.ruleForm.blogrollName
            needData.blogroll = self.ruleForm.blogroll
            needData.remark = self.ruleForm.remark
            needData.isActivity = self.ruleForm.isActivity
            self.addFlag = false
            AddOrUpdateBlogroll(needData).then(res => {
              if (res.code === 200) {
                self.$message.success(res.message)
                setTimeout(function() {
                  self.eventTab('update', 'first', 'blogroll')
                }, 500)
              } else {
                self.$message.error(res.message)
              }
            }).catch(error => {
              console.log(error)
            })
          } else {
            return
          }
        })
        setTimeout(function() {
          self.addFlag = true
        }, 3000)
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/info.scss";
</style>
